import re

# 读取日志文件并提取404错误信息
def extract_404_errors():
    errors = []
    
    try:
        with open('log/log.txt', 'r', encoding='utf-8') as f:
            content = f.read()
            
        # 使用正则表达式匹配404错误行
        pattern = r'下载图标失败 HTTP 404 -> (https://www\.shanghairanking\.cn/[^\s]+)'
        matches = re.findall(pattern, content)
        
        # 去重并排序
        errors = sorted(list(set(matches)))
        
        # 保存到log2.txt
        with open('log/log2.txt', 'w', encoding='utf-8') as f:
            f.write("异常地址列表:\n")
            f.write("=" * 50 + "\n")
            for i, url in enumerate(errors, 1):
                f.write(f"{i}. {url}\n")
        
        print(f"已提取 {len(errors)} 个异常地址并保存到 log2.txt")
        
        # 同时打印前10个作为示例
        print("\n前10个异常地址:")
        for i, url in enumerate(errors[:10], 1):
            print(f"{i}. {url}")
            
        if len(errors) > 10:
            print(f"... 还有 {len(errors) - 10} 个异常地址")
            
    except FileNotFoundError:
        print("未找到日志文件 log/log.txt")
    except Exception as e:
        print(f"处理日志文件时出错: {e}")

if __name__ == "__main__":
    extract_404_errors()